package com.jf3q.sup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.sup.domain.Menus;
import com.jf3q.sup.domain.RoleMenus;
import com.jf3q.sup.domain.Roles;
import com.jf3q.sup.dto.RoleDto;
import com.jf3q.sup.service.MenusService;
import com.jf3q.sup.service.RoleMenusService;
import com.jf3q.sup.service.RolesService;
import com.jf3q.sup.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname RoleController
 * @Date 2024/4/3 11:09
 * @Created 叶超
 */
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RolesService rolesService;
    @Autowired
    private MenusService menusService;
    @Autowired
    private RoleMenusService roleMenusService;

    @PostMapping("/getPage")
//    @PreAuthorize("hasAuthority('/roles/getPage')")
    public ResultVo getPage(@RequestBody RoleDto dto){
        Page<Roles> page = rolesService.page(new Page<Roles>(dto.getPageNum(), dto.getPageSize()), new LambdaQueryWrapper<Roles>().like(StringUtils.hasText(dto.getName()), Roles::getName, dto.getName()));
        return ResultVo.success("",page);
    }


    @GetMapping("/getMenus")
//    @PreAuthorize("hasAuthority('/roles/getMenus')")
    public ResultVo getMenus(){
        List<Menus> list = menusService.list(new LambdaQueryWrapper<Menus>().eq(Menus::getParentId,0));
        for (Menus menus : list) {
            menus.setChildren(menusService.list(new LambdaQueryWrapper<Menus>().eq(Menus::getParentId,menus.getId())));
        }
        return ResultVo.success("",list);
    }

    @GetMapping("/getMenusIds/{rolesId}")
//    @PreAuthorize("hasAuthority('/roles/getMenus')")
    public ResultVo getMenusIds(@PathVariable Integer rolesId){
        List<Integer> menusIds = new ArrayList<>();
        List<RoleMenus> list = roleMenusService.selectMenuIdList(rolesId);
        for (RoleMenus roleMenus : list) {
            menusIds.add(roleMenus.getMenuId());
        }
        return ResultVo.success("",menusIds);
    }

    @GetMapping("/fenMenus")
//    @PreAuthorize("hasAuthority('/roles/getMenus')")
    public ResultVo fenMenus(Integer rolesId,Integer... menusIds){
        try {
            roleMenusService.remove(new LambdaQueryWrapper<RoleMenus>().eq(RoleMenus::getRoleId,rolesId));
            for (Integer menusId : menusIds) {
                RoleMenus roleMenus = new RoleMenus();
                roleMenus.setRoleId(rolesId);
                roleMenus.setMenuId(menusId);
                roleMenus.setCreatedAt(new Date());
                roleMenus.setUpdatedAt(new Date());
                roleMenusService.save(roleMenus);
            }
            return ResultVo.success("分配菜单成功",null);
        } catch (Exception e) {
            throw new RuntimeException("分配菜单失败");
        }

    }


}
